<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="modelName">
              <el-input v-model="filters.libraryName" clearable placeholder="关键词搜索"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table ref="table" :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据！">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column align="center" label="id" prop="libraryId" width="80"></el-table-column>
        <el-table-column align="center" label="资讯名" prop="libraryName"></el-table-column>
        <el-table-column align="center" label="标签图片" prop="mainImage">
          <template slot-scope="scope">
            <el-image title="点击可预览" :src="scope.row.mainImage" fit="cover" :preview-src-list="[scope.row.mainImage]" :z-index="scope.$index"> </el-image>
          </template>
        </el-table-column>
        <el-table-column align="center" label="分类名称">
          <template slot-scope="scope">
            <span>{{ scope.row.firstClassifyName }}</span>
            <span v-if="scope.row.secondClassifyName">-{{ scope.row.secondClassifyName }}</span>
            <span v-if="scope.row.thirdClassifyName">-{{ scope.row.thirdClassifyName }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="地址">
          <template slot-scope="scope">
            <span>{{ scope.row.provinceName }}</span>
            <span>{{ scope.row.cityName }}</span>
            <span>{{ scope.row.areaName }}</span>
            <span>{{ scope.row.villageName }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="关键字" prop="keywords"></el-table-column>
        <el-table-column align="center" label="浏览次数" prop="viewNums"></el-table-column>
        <el-table-column align="center" label="好评/差评（次数）">
          <template slot-scope="scope">
            <span>{{ scope.row.upNums }}/{{ scope.row.downNums }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建人" prop="createUserName"></el-table-column>
        <el-table-column align="center" label="创建时间" prop="createDate"></el-table-column>

        <el-table-column label="操作" width="136" fixed="right">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import PosterModelApi from '@/api/operation/posterModel';
import informationRuleApi from '@/api/operation/InformationModel';
import * as message from '@/constants/message';
export default {
  name: 'InformationList',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      filters: {
        libraryName: '',
      },
      //服务员类别(1.商户服务员 2:公共服务员)
      operationList: [],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

      permissionAdd: 'SYSTEM_MANAGER_OPERATION_INFORMATION_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_OPERATION_INFORMATION_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_OPERATION_INFORMATION_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_OPERATION_INFORMATION_LIST_DELETE',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  watch: {
    //to去哪，from从哪来
    // $route(to,from){
    //   if(from.path =='/equip/specimenEdit' || from.path=='/equip/specimenAdd'){
    //       this.listData();
    //   }
    // }
  },
  methods: {
    init() {
      // 获取用户的角色信息
      this.listLoading = true;
      this.listData();
      this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/operation/informationAdd');
    },
    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/operation/informationAdd',
        query: { id: row.libraryId },
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/operation/informationDetail',
        query: { id: row.libraryId },
      });
    },
    handleClickDelete(val) {
      let _this = this;
      _this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        informationRuleApi
          .libraryDelete({
            id: val.libraryId,
            state: -1,
          })
          .then((res) => {
            this.$Utils.notifySuccess();
            _this.listData();
          });
      });
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      // 获取列表数据
      let cond = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
      };
      cond.libraryName = this.filters.libraryName;
      informationRuleApi
        .libraryPage(cond)
        .then((res) => {
          this.page.pageTotal = res.data.total;
          this.dataList = res.data.list;
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.listLoading = false;
          this.$refs.table.doLayout();
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
